import Vue from 'vue'
import Router from 'vue-router'
import { MessageBox } from 'element-ui'

Vue.use(Router);
/**
 * 1. 路由懒加载
 * 2. 一般首页不用懒加载
 */
const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Index',
      component: () => import('./views/login/Index.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/slogin',
      name: 'StudentLogin',
      component: () => import('./views/login/Student_Login.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/epassword',
      name: 'EditPassword',
      component: () => import('./views/login/Edit_Password.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/teacher',
      name: 'Teacher',
      component: () => import('./views/teacher/Index.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/tcpassword',
      name: 'Tcpassword',
      component: () => import('./views/teacher/ChangePassword.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/stpassword',
      name: 'Stpassword',
      component: () => import('./views/student/ChangePassword.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/student',
      name: 'Student',
      component: () => import('./views/student/index.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/evaluate',
      name: 'Evaluate',
      component: () => import('./views/student/Evaluate.vue'),
      meta: {
        keepalive: false,
      }
    },
    {
      path: '/transcript',
      name: 'Transcript',
      component: () => import('./views/student/Transcript.vue'),
      meta: {
        keepalive: false,
      }
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }
    return { x: 0, y: 0 }
  }
});

router.beforeEach((to, from, next) => {
  const nextRoute = ["Tcpassword", "Teacher"];
  const nextRoute1 = ["Evaluate", "Student", "Transcript", "Student", "Stpassword"];
  if (nextRoute.indexOf(to.name) >= 0) {
    if (localStorage.getItem('FL_TOKEN')) {
      next();
      return false;
    }
    MessageBox.alert('用户登陆超时，点击确认重新登录！').then(() => {
      router.push({ name: "Index" })
    })
    next()
  } else if (nextRoute1.indexOf(to.name) >= 0) {
    if (localStorage.getItem('FL_TOKEN')) {
      next();
      return false;
    }
    MessageBox.alert('用户登陆超时，点击确认重新登录！').then(() => {
      router.push({ name: "StudentLogin" })
    })
    next()
  } else {
    next()
  }
})
export default router
